<?php

class NotificationController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public $layout="//layouts/main";
	public function actions()
	{
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$this->pageTitle ="通知发布系统";
		if (empty(Yii::app()->user->id))
		{
			$this->redirect(array('site/loginPage'));
		}else{
			$this->render('index');
		}
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
	
	public function getName()
	{
		return Yii::app()->user->name;
	}
	
	public function actionGetUnreadCount()
	{
		$messageList=Message::model()->findAllByAttributes(
											array('recevier_id'=>Yii::app()->user->id,
												  'is_read'=>0,	
											)
										);
		echo count($messageList)==0?'':count($messageList);
	}
	
	public function actionSearchMessageList($status)
	{
		$condition=array('recevier_id'=>Yii::app()->user->id);
		if ($status=='All')
		{
			//do nothing
		}else if ($status=='0'){
			$condition['is_read']=0;
		}else if ($status=='1'){
			$condition['is_read']=1;
		}else if ($status=='2'){
			$condition=array();
			$condition['sender_id']=Yii::app()->user->id;
		}
		
		$messageList=Message::model()->findAllByAttributes(
											$condition
										);
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($messageList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$list='
				<script>messageCount='.count($messageList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>标题</td>
					<td>发送人</td>
					<td>收件人</td>
					<td>发送时间</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($messageList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T" style="display:none">';
			}
			$list=$list.'<td>'.($i+1).'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getMessageDetail('.$messageList[$i]->message_id.','.$i.')">'.$messageList[$i]->messageDetail->title.'</a></td>'
			.'<td>'.$messageList[$i]->sender->name.'</td>'
			.'<td>'.$messageList[$i]->recevier->name.'</td>'
			.'<td>'.$messageList[$i]->messageDetail->date_time.'</td>'
			.'<td>'.($messageList[$i]->is_read==1?'':('<img src="'.Yii::app()->homeUrl.'/images/mail.png"/>')).'</td>';
		}
		if (count($messageList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="6">暂无数据</td></tr></table>';
		}else{
			$list=$list.'</table>';
		}
		echo $list;
	}
	
	public function actionGetMyMessageList()
	{
		$messageList=Message::model()->findAllByAttributes(
											array('recevier_id'=>Yii::app()->user->id)
										);
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($messageList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$messageList=Message::model()->findAllByAttributes(
											array('recevier_id'=>Yii::app()->user->id)
										);
		$list='
				<script>messageCount='.count($messageList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>标题</td>
					<td>发送人</td>
					<td>收件人</td>
					<td>发送时间</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($messageList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T"style="display:none">';
			}
			$list=$list.'<td>'.($i+1).'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getMessageDetail('.$messageList[$i]->message_id.','.$i.')">'.$messageList[$i]->messageDetail->title.'</a></td>'
			.'<td>'.$messageList[$i]->sender->name.'</td>'
			.'<td>'.$messageList[$i]->recevier->name.'</td>'
			.'<td>'.$messageList[$i]->messageDetail->date_time.'</td>'
			.'<td>'.($messageList[$i]->is_read==1?'':('<img src="'.Yii::app()->homeUrl.'/images/mail.png"/>')).'</td>';
		}
		
		if (count($messageList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="6">暂无数据</td></tr></table>';
		}
		$list=$list.'</table>';
		echo $list;
	}
	
	public function actionViewMessageDetail($mid)
	{
		$message=Message::model()->findByPk($mid);
		if (empty($message))
		{
			$res=
			'<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">错误</h4>
			</div>
			<div class="modal-body">
				<div class="container">
					<div class="form-group">
						<h1>未找到该信息！</h1>
					</div>
				</div>
			</div>';
			echo $res;
			return;
		}
		
		$res='<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">消息</h4>
			</div>
			<div class="modal-body">
				<div class="input-group">
					<label for="title">发送人</label>
					<div class="container">
						<p style="color:#2f6f9f">'.$message->sender->name.'</p>
					</div>
					<div class="form-group">
						<label for="title">发送时间</label>
						<div class="container">
							<p style="color:#2f6f9f">'.$message->messageDetail->date_time.'</p>
						</div>
					</div>		
					<div class="form-group">
						<label for="title"> 标题</label>
						<div class="container">
							<p style="color:#2f6f9f">'.$message->messageDetail->title.'</p>
						</div>
					</div>              
					<div class="form-group">
						<label for="content" class="required"> 内容</label>
						<div class="container">
							<p style="color:#2f6f9f">'.$message->messageDetail->content.'</p>
						</div>
					</div>
					<table>
						<tr>
							<div class="btn-group">
								<td><a class="btn btn-success" data-dismiss="modal" aria-hidden="true" role="button">关闭</a></td>
							</div>
						</tr>
					</table>
				</div>
			</div>';
		if ($message->recevier_id==Yii::app()->user->id)
		{
			$message->is_read=1;
			$message->save();
		}
		
		echo $res;
	}
	
	public function actionGetGradeList()
	{
		$res='<option value="">请选择年级</option>';
		$list=Grade::model()->findAll();
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->grade_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function actionGetDepartmentList()
	{
		$res='<option value="">请选择院系</option>';
		$list=Department::model()->findAll();
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->department_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function actionGetMajorList($did)
	{
		$res='<option value="">请选择专业</option>';
		if (!empty($did))
		{
			if ($did!='All')
			{
				$list=Major::model()->findAllByAttributes(array('department_id'=>$did));
			}else{
				$list=Major::model()->findAll();
			}
		}else
			$list=Major::model()->findAll();
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->major_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function actionGetAllMajorList($did)
	{
		$res='<option value="All">所有</option>';
		if (!empty($did))
			$list=Major::model()->findAllByAttributes(array('department_id'=>$did));
		else
			$list=Major::model()->findAll();
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->major_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function actionGetClassList($gid,$did,$mid)
	{
		$res='<option value="">请选择班级</option>';
		$list=Classes::model()->findAllByAttributes(array(
														'grade_id'=>$gid,
														'department_id'=>$did,
														'major_id'=>$mid,
													));
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->class_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function actionGetAllClassList($gid,$did,$mid)
	{
		$res='<option value="All">所有</option>';
		$list=Classes::model()->findAllByAttributes(array(
														'grade_id'=>$gid,
														'department_id'=>$did,
														'major_id'=>$mid,
													));
		for ($i=0;$i<count($list);$i++)
		{
			$res=$res.'<option value="'.$list[$i]->class_id.'">'.$list[$i]->name.'</option>';
		}
		echo $res;
	}
	
	public function getMenu()
	{
		$menu='';
		$menuHead='<ul class="nav navbar-nav">';
		$messageMenu='<li class="dropdown">
			<a href="#" class="dropdown-toggle active" data-toggle="dropdown"><span class="badge"></span>我的通知<b class="caret"></b></a>
			<ul class="dropdown-menu">
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/index?status=0">未读通知<span class="badge"></span></a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/index?status=1">已读通知</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/index?status=2">已发通知</a></li>
			</ul>
		</li>';
		
		$systemMenu='<li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown">系统管理<b class="caret"></b></a>
			<ul class="dropdown-menu">
				<li><a href="'.Yii::app()->request->baseUrl.'/site/usermanagepage">用户管理</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/grademanagepage">年级管理</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/departmentmanagepage">院系管理</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/majormanagepage">专业管理</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/classmanagepage">班级管理</a></li>
			</ul>
		</li>';
		
		$sendMenu='<li class="dropdown">
			<a href="#" class="dropdown-toggle" data-toggle="dropdown">信息发送<b class="caret"></b></a>
			<ul class="dropdown-menu">
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/singlesendpage">单人通知发送</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/multisendpage">多人通知发送</a></li>
				<li><a href="'.Yii::app()->request->baseUrl.'/notification/groupsendpage">群体通知发送</a></li>
			</ul>
		</li>';
		
		$userMenu=
		'<ul class="nav navbar-nav navbar-right">
				<li class="dropdown">
					<a href="#" class="dropdown-toggle" data-toggle="dropdown">'.Yii::app()->user->name.'<b class="caret"></b></a>
					<ul class="dropdown-menu">';
		if (!empty(Yii::app()->user->id))
		{
				$userMenu=$userMenu.'<li><a href="'.Yii::app()->request->baseUrl.'/site/modifypage">个人信息修改</a></li>
						<li><a href="'.Yii::app()->request->baseUrl.'/site/logout">登出</a></li>';
		}
		$userMenu=$userMenu.'</ul>
								</li>
							 </ul>';
		
		$menuEnd='</ul>';
		if (!empty(Yii::app()->user->id))
		{
			$menu=$menuHead.$messageMenu.$menuEnd.$userMenu;
			$priv=User::model()->findByPk(Yii::app()->user->id)->privilege;
			if (!empty($priv))
			{
				if ($priv==2)
					$menu=$menuHead.$messageMenu.$sendMenu.$menuEnd.$userMenu;
				if ($priv==3)
					$menu=$menuHead.$messageMenu.$systemMenu.$sendMenu.$menuEnd.$userMenu;
			}
		}
		return $menu;
	}
	
	public function actionGradeManagePage()
	{
		$this->render('gradeManagePage');
	}
	
	public function actionGetGradeGrid()
	{
		$gradeList=Grade::model()->findAll();
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($gradeList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$list='
				<script>gradeCount='.count($gradeList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>名称</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($gradeList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T" style="display:none">';
			}
			$list=$list.'<td>'.($i+1).'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getGradeDetail('.$gradeList[$i]->grade_id.','.$i.')">'.$gradeList[$i]->name.'</a></td>'
			.'<td>'.($gradeList[$i]->status==1?'显示':'隐藏').'</td>';
		}
		
		if (count($gradeList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="5">暂无数据</td></tr></table>';
		}
		$list=$list.'<tr><td colspan="5"><a class="btn btn-success" style="" href="'.Yii::app()->baseUrl.'/notification/addNewGradePage">新增</a></td></tr></table>';
		echo $list;
	}
	
	public function actionGetMajorGrid()
	{
		$majorList=Major::model()->findAll();
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($majorList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$list='
				<script>majorCount='.count($majorList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>所属院系</td>
					<td>名称</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($majorList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T" style="display:none">';
			}
			$list=$list.'<td>'.($i+1).'</td>'
			.'<td>'.$majorList[$i]->department->name.'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getMajorDetail('.$majorList[$i]->major_id.','.$i.')">'.$majorList[$i]->name.'</a></td>'
			.'<td>'.($majorList[$i]->status==1?'显示':'隐藏').'</td>';
		}
		
		if (count($majorList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="5">暂无数据</td></tr></table>';
		}
		$list=$list.'<tr><td colspan="5"><a class="btn btn-success" style="" href="'.Yii::app()->baseUrl.'/notification/addNewMajorPage">新增</a></td></tr></table>';
		echo $list;
	}
	
	public function actionGetDepartmentGrid()
	{
		$departmentList=Department::model()->findAll();
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($departmentList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$list='
				<script>departmentCount='.count($departmentList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>名称</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($departmentList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T" style="display:none">';
			}
			$list=$list.'</td>'.'<td>'.($i+1).'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getDepartmentDetail('.$departmentList[$i]->department_id.')">'.$departmentList[$i]->name.'</a></td>'
			.'<td>'.($departmentList[$i]->status==1?'显示':'隐藏').'</td>';
		}
		
		if (count($departmentList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="5">暂无数据</td></tr></table>';
		}
		$list=$list.'<tr><td colspan="5"><a class="btn btn-success" style="" href="'.Yii::app()->baseUrl.'/notification/addNewDepartmentPage">新增</a></td></tr></table>';
		echo $list;
	}
	
	public function actionGetClassGrid()
	{
		$classList=Classes::model()->findAll();
		$res='<ul class="pagination"><li><a href="#" onclick="refreshList()">刷新</a></li>';
		for ($i=0;$i<count($classList)/10;$i++)
		{
		  $res=$res.'<li><a href="#" onclick="goPage('.($i+1).')">'.($i+1).'</a></li>';
		}
		$res=$res.'</ul>';
		echo $res;
		$list='
				<script>classCount='.count($classList).'</script>
				<table class="table table-hover">
				<tr>
					<td>#</td>
					<td>所属年级</td>
					<td>所属院系</td>
					<td>所属专业</td>
					<td>名称</td>
					<td>状态</td>
			    </tr>';
		for ($i=0;$i<count($classList);$i++)
		{
			if ($i<=9)
			{
				$list=$list.'<tr id="listT'.$i.'T">';
			}else{
				$list=$list.'<tr id="listT'.$i.'T" style="display:none">';
			}
			$list=$list.'<td>'.($i+1).'</td>'
			.'<td>'.$classList[$i]->grade->name.'</td>'
			.'<td>'.$classList[$i]->department->name.'</td>'
			.'<td>'.$classList[$i]->major->name.'</td>'
			.'<td><a href="#" data-toggle="modal" data-target="#detailModal" onclick="getClassDetail('.$classList[$i]->class_id.','.$i.')">'.$classList[$i]->name.'</a></td>'
			.'<td>'.($classList[$i]->status==1?'显示':'隐藏').'</td>';
		}
		
		if (count($classList)==0)
		{
			$list=$list.'<tr><td style="text-align:center" colspan="5">暂无数据</td></tr></table>';
		}
		$list=$list.'<tr><td colspan="5"><a class="btn btn-success" style="" href="'.Yii::app()->baseUrl.'/notification/addNewClassPage">新增</a></td></tr></table>';
		echo $list;
	}
	
	public function actionViewDepartmentDetail($did)
	{
		$department=Department::model()->findByPk($did);
		if (empty($department))
		{
			$res=
			'<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">错误</h4>
			</div>
			<div class="modal-body">
				<div class="container">
					<div class="form-group">
						<h1>未找到该院系！</h1>
					</div>
				</div>
			</div>';
			echo $res;
			return;
		}
		
		$res='<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">院系</h4>
			</div>
			<div class="modal-body">
				<form action="'.Yii::app()->request->baseUrl.'/notification/modifyDepartment" method="post" accept-charset="utf-8" role="form">
					<div class="input-group">
						<label for="title">院系名</label>
						<div class="form-group">
							<input type="text" name="did" value="'.$department->department_id.'"class="form-control" placeholder="院系ID" style="display:none">
							<input type="text" name="name" value="'.$department->name.'"class="form-control" placeholder="院系名" required="">
						</div>
					</div>
					<tr>
						<td><input name="modify" class="btn btn-success" type="submit" role="button" value="修改" /></td>
						<td><input name="show" class="btn btn-primary" type="submit" role="button" value="显示" /></td>
						<td><input name="hide" class="btn btn-primary" type="submit" role="button" value="隐藏" /></td>
						<td>
							<div class="btn-group">
							  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
								删除<span class="caret"></span>
								<span class="sr-only">Toggle Dropdown</span>
							  </button>
							  <ul class="dropdown-menu"  style="min-width:0px;padding:0 0 0 0;" role="menu">
								<li><input name="delete" class="btn btn-danger" type="submit" role="button" value="确认删除"></li>
							  </ul>
							</div>
						</td>
						<td><a class="btn btn-success" data-dismiss="modal" aria-hidden="true" role="button">返回</a></td> 
					</tr>
				</form>
			</div>';
		echo $res;
	}
	
	public function actionViewGradeDetail($gid)
	{
		$grade=Grade::model()->findByPk($gid);
		if (empty($grade))
		{
			$res=
			'<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">错误</h4>
			</div>
			<div class="modal-body">
				<div class="container">
					<div class="form-group">
						<h1>未找到该年级！</h1>
					</div>
				</div>
			</div>';
			echo $res;
			return;
		}
		
		$res='<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">年级</h4>
			</div>
			<div class="modal-body">
				<form action="'.Yii::app()->request->baseUrl.'/notification/modifyGrade" method="post" accept-charset="utf-8" role="form">
					<div class="input-group">
						<label for="title">年级名</label>
						<div class="form-group">
							<input type="text" name="gid" value="'.$grade->grade_id.'"class="form-control" placeholder="年级名" style="display:none">
							<input type="text" name="name" value="'.$grade->name.'"class="form-control" placeholder="年级名" required="">
						</div>
					</div>
					<tr>
						<td><input name="modify" class="btn btn-success" type="submit" role="button" value="修改" /></td>
						<td><input name="show" class="btn btn-primary" type="submit" role="button" value="显示" /></td>
						<td><input name="hide" class="btn btn-primary" type="submit" role="button" value="隐藏" /></td>
						<td>
							<div class="btn-group">
							  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
								删除<span class="caret"></span>
								<span class="sr-only">Toggle Dropdown</span>
							  </button>
							  <ul class="dropdown-menu"  style="min-width:0px;padding:0 0 0 0;" role="menu">
								<li><input name="delete" class="btn btn-danger" type="submit" role="button" value="确认删除"></li>
							  </ul>
							</div>
						</td>
						<td><a class="btn btn-success" data-dismiss="modal" aria-hidden="true" role="button">返回</a></td> 
					</tr>
				</form>
			</div>';
		echo $res;
	}
	
	public function actionViewMajorDetail($mid)
	{
		$major=Major::model()->findByPk($mid);
		if (empty($major))
		{
			$res=
			'<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">错误</h4>
			</div>
			<div class="modal-body">
				<div class="container">
					<div class="form-group">
						<h1>未找到该专业！</h1>
					</div>
				</div>
			</div>';
			echo $res;
			return;
		}
		
		$classList=Grade::model()->findAll();
		$list=Department::model()->findAll();
			$selectdepartment='<option value="">请选择所属院系</option>';
			for ($i=0;$i<count($list);$i++)
			{
				if ($list[$i]->department_id==$major->department_id)
					$selectdepartment=$selectdepartment.'<option value="'.$list[$i]->department_id.'" selected>'.$list[$i]->name.'</option>';
				else
					$selectdepartment=$selectdepartment.'<option value="'.$list[$i]->department_id.'">'.$list[$i]->name.'</option>';
			}
		$res='<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">专业</h4>
			</div>
			<div class="modal-body">
				<form action="'.Yii::app()->request->baseUrl.'/notification/modifyMajor" method="post" accept-charset="utf-8" role="form">
					<div class="input-group">
						<label for="title">专业名</label>
						<div class="form-group">
							<input type="text" name="mid" value="'.$major->major_id.'"class="form-control" placeholder="专业ID" style="display:none">
							<select type="text" name="department" class="form-control" placeholder="院系名" required>'.$selectdepartment.'</select>
							<input type="text" name="name" value="'.$major->name.'"class="form-control" placeholder="专业名" required="">
						</div>
					</div>
					<tr>
						<td><input name="modify" class="btn btn-success" type="submit" role="button" value="修改" /></td>
						<td><input name="show" class="btn btn-primary" type="submit" role="button" value="显示" /></td>
						<td><input name="hide" class="btn btn-primary" type="submit" role="button" value="隐藏" /></td>
						<td>
							<div class="btn-group">
							  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
								删除<span class="caret"></span>
								<span class="sr-only">Toggle Dropdown</span>
							  </button>
							  <ul class="dropdown-menu"  style="min-width:0px;padding:0 0 0 0;" role="menu">
								<li><input name="delete" class="btn btn-danger" type="submit" role="button" value="确认删除"></li>
							  </ul>
							</div>
						</td>
						<td><a class="btn btn-success" data-dismiss="modal" aria-hidden="true" role="button">返回</a></td> 
					</tr>
				</form>
			</div>';
		echo $res;
	}
	
	public function actionViewClassDetail($cid)
	{
		$class=Classes::model()->findByPk($cid);
		if (empty($class))
		{
			$res=
			'<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">错误</h4>
			</div>
			<div class="modal-body">
				<div class="container">
					<div class="form-group">
						<h1>未找到该班级！</h1>
					</div>
				</div>
			</div>';
			echo $res;
			return;
		}
		
		$gradeList=Grade::model()->findAll();
		$selectgrade='';
		for ($i=0;$i<count($gradeList);$i++)
		{
			if ($class->grade_id==$gradeList[$i]->grade_id)
				$selectgrade=$selectgrade.'<option value="'.$gradeList[$i]->grade_id.'" selected>'.$gradeList[$i]->name.'</option>';
			else
				$selectgrade=$selectgrade.'<option value="'.$gradeList[$i]->grade_id.'">'.$gradeList[$i]->name.'</option>';
		}
		
		$departmentList=Department::model()->findAll();
		$selectdepartment='';
		for ($i=0;$i<count($departmentList);$i++)
		{
			if ($class->department_id==$departmentList[$i]->department_id)
				$selectdepartment=$selectdepartment.'<option value="'.$departmentList[$i]->department_id.'" selected>'.$departmentList[$i]->name.'</option>';
			else
				$selectdepartment=$selectdepartment.'<option value="'.$departmentList[$i]->department_id.'">'.$departmentList[$i]->name.'</option>';
		}
		
		$majorList=Major::model()->findAllByAttributes(array('department_id'=>$class->department_id));
		$selectmajor='';
		for ($i=0;$i<count($majorList);$i++)
		{
			if ($class->major_id==$majorList[$i]->major_id)
				$selectmajor=$selectmajor.'<option value="'.$majorList[$i]->major_id.'" selected>'.$majorList[$i]->name.'</option>';
			else
				$selectmajor=$selectmajor.'<option value="'.$majorList[$i]->major_id.'">'.$majorList[$i]->name.'</option>';
		}
		
		$res='<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h4 class="modal-title" id="myModalLabel">班级</h4>
			</div>
			<div class="modal-body">
				<form action="'.Yii::app()->request->baseUrl.'/notification/modifyClass" method="post" accept-charset="utf-8" role="form">
					<div class="input-group">
						<label for="title">所属年级</label>
						<div class="form-group">
							<select id="grade" name="grade"">
							'.$selectgrade.'
							</select>
						</div>
					</div>
					<div class="input-group">
						<label for="title">所属院系</label>
						<div class="form-group">
							<select id="department" name="department" class="form-control" onchange="getMajorList(this.value)">
							'.$selectdepartment.'
							</select>
						</div>
					</div>
					<div class="input-group">
						<label for="title">所属专业</label>
						<div class="form-group">
							<select id="major" name="major" class="form-control">
							'.$selectmajor.'
							</select>
						</div>
					</div>
					<div class="input-group">
						<label for="title">班级名称</label>
						<div class="form-group">
							<input type="text" name="cid" value="'.$class->class_id.'"class="form-control" placeholder="班级ID" style="display:none">
							<input type="text" name="name" value="'.$class->name.'"class="form-control" placeholder="班级名" required="">
						</div>
					</div>
					<tr>
						<td><input name="modify" class="btn btn-success" type="submit" role="button" value="修改" /></td>
						<td><input name="show" class="btn btn-primary" type="submit" role="button" value="显示" /></td>
						<td><input name="hide" class="btn btn-primary" type="submit" role="button" value="隐藏" /></td>
						<td>
							<div class="btn-group">
							  <button type="button" class="btn btn-danger dropdown-toggle" data-toggle="dropdown">
								删除<span class="caret"></span>
								<span class="sr-only">Toggle Dropdown</span>
							  </button>
							  <ul class="dropdown-menu"  style="min-width:0px;padding:0 0 0 0;" role="menu">
								<li><input name="delete" class="btn btn-danger" type="submit" role="button" value="确认删除"></li>
							  </ul>
							</div>
						</td>
						<td><a class="btn btn-success" data-dismiss="modal" aria-hidden="true" role="button">返回</a></td> 
					</tr>
				</form>
			</div>';
		echo $res;
	}
	
	public function actionAddNewClass()
	{
		if (empty($_POST['name'])||empty($_POST['grade'])||empty($_POST['major'])||empty($_POST['department']))
		{
			$error='请填写完整信息!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$class=new Classes;
		$class->name=$_POST['name'];
		$class->grade_id=$_POST['grade'];
		$class->department_id=$_POST['department'];
		$class->major_id=$_POST['major'];
		$class->status=1;
		if ($class->save()>0)
		{
			$this->redirect(array('notification/classManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionModifyGrade()
	{
		if (isset($_POST['modify']))
		{
			$grade=Grade::model()->findByPk($_POST['gid']);
			$grade->name=$_POST['name'];
			$grade->save();
			$this->redirect(array('notification/gradeManagePage'));
		}else if (isset($_POST['delete'])){
			$grade=Grade::model()->findByPk($_POST['gid']);
			$grade->delete();
			$this->redirect(array('notification/gradeManagePage'));
		}else if (isset($_POST['show'])){
			$grade=Grade::model()->findByPk($_POST['gid']);
			$grade->status=1;
			$grade->save();
			$this->redirect(array('notification/gradeManagePage'));
		}else if (isset($_POST['hide'])){
			$grade=Grade::model()->findByPk($_POST['gid']);
			$grade->status=0;
			$grade->save();
			$this->redirect(array('notification/gradeManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionAddNewMajorPage()
	{
		$this->render('addNewMajorPage');
	}
	
	public function actionAddNewMajor()
	{
		if (empty($_POST['name']))
		{
			$error='请填写完整信息!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$major=new Major;
		$major->name=$_POST['name'];
		$major->department_id=$_POST['department'];
		$major->status=1;
		if ($major->save()>0)
		{
			$this->redirect(array('notification/majorManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionModifyMajor()
	{
		if (isset($_POST['modify']))
		{
			$major=Major::model()->findByPk($_POST['mid']);
			$major->name=$_POST['name'];
			$major->department_id=$_POST['department'];
			$major->save();
			$this->redirect(array('notification/majorManagePage'));
		}else if (isset($_POST['delete'])){
			$major=Major::model()->findByPk($_POST['mid']);
			$major->delete();
			$this->redirect(array('notification/majorManagePage'));
		}else if (isset($_POST['show'])){
			$major=Major::model()->findByPk($_POST['mid']);
			$major->status=1;
			$major->save();
			$this->redirect(array('notification/majorManagePage'));
		}else if (isset($_POST['hide'])){
			$major=Major::model()->findByPk($_POST['mid']);
			$major->status=0;
			$major->save();
			$this->redirect(array('notification/majorManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionModifyDepartment()
	{
		if (isset($_POST['modify']))
		{
			$department=Department::model()->findByPk($_POST['did']);
			$department->name=$_POST['name'];
			$department->save();
			$this->redirect(array('notification/departmentManagePage'));
		}else if (isset($_POST['delete'])){
			$department=Department::model()->findByPk($_POST['did']);
			$department->delete();
			$this->redirect(array('notification/departmentManagePage'));
		}else if (isset($_POST['show'])){
			$department=Department::model()->findByPk($_POST['did']);
			$department->status=1;
			$department->save();
			$this->redirect(array('notification/departmentManagePage'));
		}else if (isset($_POST['hide'])){
			$department=Department::model()->findByPk($_POST['did']);
			$department->status=0;
			$department->save();
			$this->redirect(array('notification/departmentManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionModifyClass()
	{
		if (isset($_POST['modify']))
		{
			$class=Classes::model()->findByPk($_POST['cid']);
			$class->name=$_POST['name'];
			$class->grade_id=$_POST['grade'];
			$class->department_id=$_POST['department'];
			$class->major_id=$_POST['major'];
			$class->save();
			$this->redirect(array('notification/classManagePage'));
		}else if (isset($_POST['delete'])){
			$class=Classes::model()->findByPk($_POST['cid']);
			$class->delete();
			$this->redirect(array('notification/classManagePage'));
		}else if (isset($_POST['show'])){
			$class=Classes::model()->findByPk($_POST['cid']);
			$class->status=1;
			$class->save();
			$this->redirect(array('notification/classManagePage'));
		}else if (isset($_POST['hide'])){
			$class=Classes::model()->findByPk($_POST['cid']);
			$class->status=0;
			$class->save();
			$this->redirect(array('notification/classManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionAddNewGradePage()
	{
		$this->render('addNewGradePage');
	}
	
	public function actionAddNewClassPage()
	{
		$this->render('addNewClassPage');
	}
	
	public function actionAddNewGrade()
	{
		if (empty($_POST['name']))
		{
			$error='请填写完整信息!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$grade=new Grade;
		$grade->name=$_POST['name'];
		$grade->status=1;
		if ($grade->save()>0)
		{
			$this->redirect(array('notification/gradeManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionAddNewDepartment()
	{
		if (empty($_POST['name']))
		{
			$error='请填写完整信息!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$department=new Department;
		$department->name=$_POST['name'];
		$department->status=1;
		if ($department->save()>0)
		{
			$this->redirect(array('notification/departmentManagePage'));
		}else{
			$error='未定义错误!';
			$this->render('error', array('error'=>$error));
			return;
		}
	}
	
	public function actionMajorManagePage()
	{
		$this->render('majorManagePage');
	}
	
	public function actionDepartmentManagePage()
	{
		$this->render('departmentManagePage');
	}
	
	public function actionAddNewDepartmentPage()
	{
		$this->render('addNewDepartmentPage');
	}
	
	public function actionClassManagePage()
	{
		$this->render('classManagePage');
	}
	
	public function actionSingleSendPage()
	{
		$data=array();
		$userList=User::model()->findAll();
		for ($i=0;$i<count($userList);$i++)
		{
			$data[$i]='['.$userList[$i]->ID.']:'.$userList[$i]->name;
		}
		$this->render('singleSendPage',array('data' => $data,));
	}
	
	public function actionSingleSend()
	{
		$toID=substr($_POST['to'],1,strpos($_POST['to'],"]:")-1);
		$toUser=User::model()->findByAttributes(array('ID'=>$toID));
		if (empty($toUser))
		{
			$error='发送对象不正确!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$detail=new MessageDetail;
		$detail->title=$_POST['title'];
		$detail->content=$_POST['content'];
		$detail->date_time=date('Y-m-d H:i:s',time());
		$detail->save();
		
		$message=new Message;
		$message->sender_id=Yii::app()->user->id;
		$message->recevier_id=$toUser->user_id;
		$message->detail=$detail->detail_id;
		$message->is_read=0;
		$message->save();
		
		$this->redirect(array('notification/index?status=2'));
	}
	
	public function actionMultiSend()
	{
		$userList=explode(';',$_POST['all']);
		//检测是否数据正常
		foreach($userList as $user){
			if (empty($user))
				continue;
			$toID=substr($user,1,strpos($user,"]:")-1);
			$toUser=User::model()->findByAttributes(array('ID'=>$toID));
			if (empty($toUser))
			{
				$error='发送对象不正确!';
				$this->render('error', array('error'=>$error));
				return;
			}
		}
		//数据库中只需要保存一条就行
		$detail=new MessageDetail;
		$detail->title=$_POST['title'];
		$detail->content=$_POST['content'];
		$detail->date_time=date('Y-m-d H:i:s',time());
		$detail->save();
		//群发
		foreach($userList as $user){
			if (empty($user))
				continue;
			$toID=substr($user,1,strpos($user,"]:")-1);
			$toUser=User::model()->findByAttributes(array('ID'=>$toID));
			$message=new Message;
			$message->sender_id=Yii::app()->user->id;
			$message->recevier_id=$toUser->user_id;
			$message->detail=$detail->detail_id;
			$message->is_read=0;
			$message->save();
		}
		
		$this->redirect(array('notification/index?status=2'));
	}
	
	public function actionGroupSend()
	{
		$toUser=null;
		if ($_POST['class']!='All')
			$toUser=User::model()->findByAttributes(array('class'=>$_POST['class']));
		else{
			$condition=array();
			if ($_POST['grade']!='All')
			{
				$condition['grade_id']=$_POST['grade'];
			}
			if ($_POST['department']!='All'){
				$condition['department_id']=$_POST['department'];
			}
			if ($_POST['major']!='All'){
				$condition['major_id']=$_POST['major'];
			}
			
			if ($_POST['class']!='All'){
				$toClass=Classes::model()->findAllByAttributes(array('class_id'=>$_POST['class']));
			}else{
				$toClass=Classes::model()->findAllByAttributes($condition);
			}
			
			$toUser=array();
			if (!empty($toClass))
			{	
				for ($i=0;$i<count($toClass);$i++)
					$toUser=array_merge($toUser,User::model()->findAllByAttributes(array('class'=>$toClass[$i]->class_id)));
			}
		}
		
		if (empty($toUser))
		{
			$error='发送对象为空!';
			$this->render('error', array('error'=>$error));
			return;
		}
		
		$detail=new MessageDetail;
		$detail->title=$_POST['title'];
		$detail->content=$_POST['content'];
		$detail->date_time=date('Y-m-d H:i:s',time());
		$detail->save();
		
		for ($i=0;$i<count($toUser);$i++)
		{
			$message=new Message;
			$message->sender_id=Yii::app()->user->id;
			$message->recevier_id=$toUser[$i]->user_id;
			$message->detail=$detail->detail_id;
			$message->is_read=0;
			$message->save();
		}
		
		$this->redirect(array('notification/index?status=2'));
	}
	
	public function actionMultiSendPage()
	{
		$data=array();
		$userList=User::model()->findAll();
		for ($i=0;$i<count($userList);$i++)
		{
			$data[$i]='['.$userList[$i]->ID.']:'.$userList[$i]->name;
		}
		$this->render('multiSendPage',array('data' => $data,));
	}
	
	public function actionGroupSendPage()
	{
		$this->render('groupSendPage');
	}
}